function msgbox(msg, type){
	
	if(type == "ok"){
		layer.msg(msg, {icon: 1});
	}
	
	if(type == "fail"){
		layer.msg(msg, {shift: 6, time: 2000, icon: 2});
	}
	
	if(type == "warn"){
		layer.msg(msg, {shift: 6, time: 2000, icon: 7});
	}
	
	if(type == undefined){
		layer.msg(msg, {shift: 6, time: 2000});
	}
}

function closeIframe(){
	var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
	parent.layer.close(index);
}


function showUserSelection(multiple,selectedValues,selectedNames){
	
	if(multiple){
		title = "请选择用户(多选)";
	}else{
		title = "请选择用户(单选)";
	}
	layer.open({
		type: 2,
		title: title,
		area: ['820px', '550px'], //宽高
		skin: 'layui-layer-lan',
		content: ctx + '/user/select?selection=user&multiple='+multiple+'&selectedValues='+selectedValues+'&selectedNames='+selectedNames
    });
}

function showRoleSelection(multiple,selectedValues){
	
	if(multiple){
		title = "请选择角色(多选)";
	}else{
		title = "请选择角色(单选)";
	}
	layer.open({
		type: 2,
		title: title,
		area: ['700px', '500px'], //宽高
		skin: 'layui-layer-lan',
		content: ctx + '/role/select?selection=role&multiple='+multiple+'&selectedValues='+selectedValues
    });
}

function showDepartmentSelection(multiple,selectedValues){
	
	if(multiple){
		title = "请选择部门(多选)";
	}else{
		title = "请选择部门(单选)";
	}
	layer.open({
		type: 2,
		title: title,
		area: ['700px', '500px'], //宽高
		skin: 'layui-layer-lan',
		content: ctx + '/department/select?selection=department&multiple='+multiple+'&selectedValues='+selectedValues
    });
}

function showGroupSelection(multiple,selectedValues){
	
	if(multiple){
		title = "请选择用户组(多选)";
	}else{
		title = "请选择用户组(单选)";
	}
	layer.open({
		type: 2,
		title: title,
		area: ['700px', '500px'], //宽高
		skin: 'layui-layer-lan',
		content: ctx + '/group/select?selection=group&multiple='+multiple+'&selectedValues='+selectedValues
    });
}


function addSelectedValues(multiple, selectedValues){
	if(selectedValues.length >0 && selectedValues != null && selectedValues != ''){

		if(!multiple){
			$.jstree.reference($("#organizationTree")).check_node('#'+selectedValues);
		}else {
			if (selectedValues.indexOf(",") !=-1) {
				var values = selectedValues.split(",");
				for(var i=0; i< values.length; i++){
					$.jstree.reference($("#organizationTree")).check_node('#'+values[i]);
				}
			} else {
				$.jstree.reference($("#organizationTree")).check_node('#'+selectedValues);
			}
		}
	}
}

function addCheckedOption(nodeId, nodeText, multiple){
	if(!multiple){
		$("#organizationTreeRight span").each(function(index, elem){
		    node = this.id;
		    if(nodeId != node){
		    	$("span[id='"+node+"']").remove();
		    	$.jstree.reference($('#organizationTree')).uncheck_node('#'+node);
		    }
		});
	}
	appenOptionToRight(nodeId, nodeText);
}

function appenOptionToRight(id,text){
	$('#organizationTreeRight:not(:has("#'+id+'"))').append("<span id='"+id+"' name='"+text+"'><a onclick='removeClosedOption(\""+id+"\");'></a>&nbsp;"+text+"</span>");
}

function removeUnCheckedOption(nodeId){
	removeClosedOption(nodeId);
}

function removeClosedOption(nodeId){
	$("span[id='"+nodeId+"']").remove();
	var status = $.jstree.reference($('#organizationTree')).is_checked('#'+nodeId);
	
 	if(status == true){
 		$.jstree.reference($('#organizationTree')).uncheck_node('#'+nodeId);
 	}
}

function addSelectedOption(selection){

	var selectedValues = [];
	var selectedNames = [];
	
	$("#organizationTreeRight span").each(function(index, elem){
		selectedValues.push($(this).attr("id"));
		selectedNames.push($(this).text());
	});
	
	parent.doSelect(selectedValues,selectedNames,selection);
	
	closeIframe();
}